import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col} from 'antd';
import {DATE_FORMAT} from '../constants';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import {vehicleProperty, vehiclePurchasePurpose, vehicleClaimType, powerType, transmissionType, leftRight} from '../Enum';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';
const {Description} = DescriptionList;

export class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
    };
    componentDidMount() {
        this.props.init();
    }
    render() {
        const {isDealerUser} = this.props;
        const {formatMessage} = this.props.intl;
        return (
            <div className="detail">
                <Card loading={this.state.loading}>
                    <DescriptionList size="large" title={<Row><Col span={12}>{
                        formatMessage({
                            id: 'detailPanel.card.title',
                            defaultMessage: '售后车辆信息'
                        })
                    }</Col>
                    </Row>}>
                        { !isDealerUser && <Description term={formatMessage({
                            id: 'detailPanel.data.wholesaleDealerCode',
                            defaultMessage: '总代编号',
                        })}>{this.props.detail.wholesaleDealerCode}</Description>}
                        { !isDealerUser && <Description term={formatMessage({
                            id: 'detailPanel.data.wholesaleDealerName',
                            defaultMessage: '总代名称',
                        })}>{this.props.detail.wholesaleDealerName}</Description>}
                        { !isDealerUser && <Description term={formatMessage({
                            id: 'detailPanel.data.countryName',
                            defaultMessage: '国家',
                        })}>{this.props.detail.countryName}</Description>}
                        { !isDealerUser && <Description term={formatMessage({
                            id: 'detailPanel.data.registrationCity	',
                            defaultMessage: '城市',
                        })}>{this.props.detail.registrationCity	}</Description>}
                        <Description term={formatMessage({
                            id: 'detailPanel.data.salesDealerCode',
                            defaultMessage: '售出经销商编号',
                        })}>{this.props.detail.salesDealerCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.salesDealerName',
                            defaultMessage: '售出经销商名称',
                        })}>{this.props.detail.salesDealerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.vin',
                            defaultMessage: 'VIN',
                        })}>{this.props.detail.vin}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.productCategoryCode',
                            defaultMessage: '车系',
                        })}>{this.props.detail.productCategoryCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.annoucementNumber',
                            defaultMessage: '公告号',
                        })}>{this.props.detail.annoucementNumber}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.batchNumber',
                            defaultMessage: '批次号',
                        })}>{this.props.detail.batchNumber}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isKD',
                            defaultMessage: 'CBU/KD',
                        })}>{this.props.detail.isKD ? 'KD' : 'CBU'}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.productCode',
                            defaultMessage: '产品编号',
                        })}>{this.props.detail.productCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.productName',
                            defaultMessage: '产品名称',
                        })}>{this.props.detail.productName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.engineCode',
                            defaultMessage: '发动机号',
                        })}>{this.props.detail.engineCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.transmissionSn',
                            defaultMessage: '变速箱号',
                        })}>{this.props.detail.transmissionSn}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.engineModel',
                            defaultMessage: '发动机型号',
                        })}>{this.props.detail.engineModel}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.transmissionModel',
                            defaultMessage: '变速箱型号',
                        })}>{this.props.detail.transmissionModel}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.transmission',
                            defaultMessage: '手自动',
                        })}>{this.props.detail.transmission}</Description>
                        {/* <Description term={formatMessage({
                            id: 'detailPanel.data.drive',
                            defaultMessage: '驱动',
                        })}>{this.props.detail.drive}</Description> */}
                        <Description term={formatMessage({
                            id: 'detailPanel.data.ludder',
                            defaultMessage: '左右舵	',
                        })}>{conventEnumValueToString(leftRight, this.props.detail.lfetOrRight)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.engineCylinder',
                            defaultMessage: '排量',
                        })}>{this.props.detail.engineCylinder}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.color',
                            defaultMessage: '颜色',
                        })}>{this.props.detail.color}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.rolloutDate',
                            defaultMessage: '生产日期',
                        })}>{formatDateTime(this.props.detail.rolloutDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.inboundDate',
                            defaultMessage: '入库日期',
                        })}>{formatDateTime(this.props.detail.inboundDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.shipDate',
                            defaultMessage: '发车日期',
                        })}>{formatDateTime(this.props.detail.shipDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.saleDate',
                            defaultMessage: '实销日期',
                        })}>{formatDateTime(this.props.detail.saleDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.licensePlate',
                            defaultMessage: '车牌号',
                        })}>{this.props.detail.licensePlate}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.mileage',
                            defaultMessage: '行驶里程',
                        })}>{this.props.detail.mileage}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.keyCode',
                            defaultMessage: '钥匙号',
                        })}>{this.props.detail.keyCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.certificateDate',
                            defaultMessage: '合格证日期',
                        })}>{formatDateTime(this.props.detail.certificateDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.certificateNumber',
                            defaultMessage: '合格证号',
                        })}>{this.props.detail.certificateNumber}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.salePrice',
                            defaultMessage: '实销价格',
                        })}>{formatAmount(this.props.detail.salePrice)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.saleInvoiceNumber',
                            defaultMessage: '实销发票号',
                        })}>{this.props.detail.saleInvoiceNumber}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.invoiceDate',
                            defaultMessage: '开发票日期',
                        })}>{formatDateTime(this.props.detail.invoiceDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.vehicleDeliveryDate',
                            defaultMessage: '交车日期',
                        })}>{formatDateTime(this.props.detail.vehicleDeliveryDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.vehicleProperty',
                            defaultMessage: '车辆属性',
                        })}>{conventEnumValueToString(vehicleProperty, this.props.detail.vehicleProperty)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.purchasePurpose',
                            defaultMessage: '购车用途',
                        })}>{conventEnumValueToString(vehiclePurchasePurpose, this.props.detail.purchasePurpose)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.manufacturer',
                            defaultMessage: '生产工厂',
                        })}>{this.props.detail.manufacturer}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.vehicleClaimType',
                            defaultMessage: '索赔类别',
                        })}>{conventEnumValueToString(vehicleClaimType, this.props.detail.vehicleClaimType)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.powerType',
                            defaultMessage: '动力类型',
                        })}>{conventEnumValueToString(powerType, this.props.detail.powerType)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.transmissionType',
                            defaultMessage: '变速箱类型',
                        })}>{conventEnumValueToString(transmissionType, this.props.detail.transmissionType)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isNewPower',
                            defaultMessage: '是否新能源',
                        })}>{this.props.detail.isNewPower ? formatMessage({
                                id: 'detailPanel.data.yes',
                                defaultMessage: '是',
                            }) : formatMessage({
                                id: 'detailPanel.data.no',
                                defaultMessage: '否',
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isOtherBrand',
                            defaultMessage: '是否他牌车',
                        })}>{this.props.detail.isOtherBrand ? formatMessage({
                                id: 'detailPanel.data.yes',
                                defaultMessage: '是',
                            }) : formatMessage({
                                id: 'detailPanel.data.no',
                                defaultMessage: '否',
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.remark',
                            defaultMessage: '备注',
                        })}>{this.props.detail.remark}</Description>
                    </DescriptionList>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.state.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPanel.btn.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {getDetail, tableSearch} from './actions';
import {injectIntl} from './intl';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);
const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    permissions: getPermissions(state),
    detail: getDetailData(state),
    isDealerUser: state.getIn(['page', 'domainData', 'init', 'isDealerUser']),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    onCancel: () => {
        props.history.push(routes.query.url());
    },
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
